#!/usr/bin/env node
//网课
// const events = require('events');

// function Dog(name) {
//   events.EventEmitter.call(this);

//   let self = this;
//   this.name = name;

//   let timer = setInterval(function () {
//     self.emit('bark');
//   }, 1000);

//   this.stop = function () {
//     clearInterval(timer);
//   }
// }

// Dog.prototype.__proto__ = events.EventEmitter.prototype;

// module.exports = Dog;
import { EventEmitter } from 'events';

class Dog extends EventEmitter {
  #name;
  #energy;

  constructor(name, energy) {
    super();
    this.#name = name;
    this.#energy = energy;

    if (this.#energy > 0) {
      this.#bark();
    }
  }

  #bark() {
    setInterval(() => {
      if (this.#energy > 0) {
        this.emit('bark', this.#name, this.#energy);
        this.#energy--;
      } else {
        clearInterval(this.#bark);
      }
    }, 1000);
  }

  set energy(value) {
    this.#energy = value;
    if (this.#energy > 0) {
      this.#bark();
    }
  }

  get energy() {
    return this.#energy;
  }

  get name() {
    return this.#name;
  }
}

export { Dog };
